package com.tbit.uqbike.client.util;

import lombok.extern.slf4j.Slf4j;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;

import java.io.StringReader;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 微信支付工具类
 *
 * @author Leon
 * 2015-11-10 下午06:17:04
 */
@Slf4j
public class WeixinPayUtil {
    /**
     * description: 解析微信通知xml
     *
     * @param xml
     * @return
     * @author ex_yangxiaoyi
     * @see
     */
    @SuppressWarnings({"unchecked"})
    public static Map parseXmlToList2(String xml) {
        Map retMap = new HashMap();
        try {
            StringReader read = new StringReader(xml);
            // 创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
            InputSource source = new InputSource(read);
            // 创建一个新的SAXBuilder
            SAXBuilder sb = new SAXBuilder();
            sb.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
            sb.setFeature("http://xml.org/sax/features/external-general-entities", false);
            sb.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
            sb.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
            // 通过输入源构造一个Document
            Document doc = (Document) sb.build(source);
            Element root = doc.getRootElement();// 指向根节点
            List<Element> es = root.getChildren();
            if (es != null && es.size() != 0) {
                for (Element element : es) {
                    retMap.put(element.getName(), element.getValue());
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return retMap;
    }

    /**
     * 获取随机字符串
     *
     * @return
     */
    public static String getNonceStr(Boolean deposit) {
        // 6位随机数
        String strRandom = TenpayUtil.buildRandom(6) + "";
        if (deposit) {
            return "GXBIKEDEPOSIT" + DateTimeUtil.getNowTimeyyyyMMdd() + strRandom;
        } else {
            return "GXBIKE" + DateTimeUtil.getDaformatyyyyMMddHHmmss(new Date()) + strRandom;
        }
    }

    /**
     * 获取退还押金订单
     *
     * @return
     */
    public static String getReturnNonceStr() {
        // 6位随机数
        String strRandom = TenpayUtil.buildRandom(6) + "";
        // 10位序列号,可以自行调整。
        return "GXBIKERETURN" + DateTimeUtil.getNowTimeyyyyMMdd() + strRandom;
    }

    /**
     * 获取多媒体随机字符串
     *
     * @return
     */
    public static String getMediaNonceStr() {
        // 四位随机数
        String strRandom = TenpayUtil.buildRandom(6) + "";
        // 10位序列号,可以自行调整。
        return "RZ" + DateTimeUtil.getNowTimeyyyyMMdd() + strRandom;
    }

    /**
     * 元转换成分
     *
     * @param money
     * @return
     */
    public static String getMoney(String amount) {
        if (amount == null) {
            return "";
        }
        // 金额转化为分为单位
        String currency = amount.replaceAll("\\$|\\￥|\\,", "");  //处理包含, ￥ 或者$的金额  
        int index = currency.indexOf(".");
        int length = currency.length();
        Long amLong = 0L;
        if (index == -1) {
            amLong = Long.valueOf(currency + "00");
        } else if (length - index >= 3) {
            amLong = Long.valueOf((currency.substring(0, index + 3)).replace(".", ""));
        } else if (length - index == 2) {
            amLong = Long.valueOf((currency.substring(0, index + 2)).replace(".", "") + 0);
        } else {
            amLong = Long.valueOf((currency.substring(0, index + 1)).replace(".", "") + "00");
        }
        return amLong.toString();
    }


    public static void main(String[] args) {

        for (int i = 0; i < 18; i++) {
            System.out.println(getNonceStr(false));
        }
    }

}
